package com.dave.gd.core.base.entity.impl;

import java.io.Serializable;

import com.dave.gd.common.util.NumberUtils;
import com.dave.gd.core.base.entity.IAttrSpec;
import com.dave.gd.core.base.entity.IAttrValue;
import com.dave.gd.core.contants.BaseUnitConstants;

/**
 */
public abstract class EntityInstAttr<ID extends Serializable> extends AbstractBaseEntity<ID> {
    
	private static final long serialVersionUID = 2272370408827434737L;

	private AbstractDynamicBaseEntity ownerEntity;
    
    private IAttrSpec                 attrSpec;
    
    private IAttrValue                attrValue;
    
    /**
     * 用以表示一个是否新增实例
     * 	动态属性先引入，abstractBaseEntity若又需要可以引入
     * .
     */
    private boolean 				  isNewEntity = false;
    
    public abstract void setOwnerEntityId(Serializable id);
    
    public abstract Serializable getOwnerEntityId();
    
    public abstract void setAttrValueId(Long attrValueId);
    
    public abstract Long getAttrValueId();
    
    public abstract void setAttrValue(String attrValue);
    
    public abstract String getAttrValue();
    
    public abstract void setAttrId(Long attrId);
    
    public abstract Long getAttrId();
    
    public boolean getIsNewEnitity() {
    	return isNewEntity;
    }
    
    public void setIsNewEntity(boolean isNewEntity) {
    	this.isNewEntity = isNewEntity;
    }

    public IAttrSpec getAttrSpec() {
        return this.ownerEntity.getModelClass().getAttrSpecById(this.getAttrId());
    }
    
    public void setAttrSpec(IAttrSpec attrSpec) {
        this.attrSpec = attrSpec;
        this.setAttrId(attrSpec.getId());
    }
    
    public AbstractDynamicBaseEntity getOwnerEntity() {
        return this.ownerEntity;
    }
    
    public void setOwnerEntity(AbstractDynamicBaseEntity entity) {
        this.ownerEntity = entity;
        if (entity != null) {
            this.setOwnerEntityId(entity.getId());
        }
    }
    
    public Object getValue() {
        return this.getAttrSpec().convertToUI(this.getAttrValue());
    }
    
    public void setValue(Object value) {
    	this.setAttrValue(this.getAttrSpec().convertToBean(value));
    	//  T3(枚举类型)类型的attrValueId设置
    	if (BaseUnitConstants.SELECT_ATTR
    			.equals(this.getAttrSpec().getAttrType())) {
    		this.setAttrValueId(NumberUtils.nullToLongZero(value));
    	}
    }
    
    public EntityInstAttr() {
    	
    }
    
    public EntityInstAttr(Boolean genId) {
    	super(genId);
    }
    
}
